﻿#include "cuc/src/cwkshell/view/frame/lib/foo.h"

hgeResourceManager *g_res=NULL;

bool FrameFunc(void);
bool RenderFunc(void);

CHgeView::CHgeView(void)
{
}

CHgeView::~CHgeView(void)
{
}

bool CHgeView::CreateHge( const RECT& rct, HWND hwnd )
{
	try
	{
		g_lpHGE = hgeCreate(HGE_VERSION);
		if(g_lpHGE == NULL)
		{
			::MessageBoxA(NULL, "创建hge失败.", "HGE error",MB_OK|MB_ICONERROR|MB_SYSTEMMODAL); 
			return false;
		}

		//	g_lpHGEGUI	= new hgeGUI();
		g_lpView = this;		
		parent_hwnd_= hwnd;

		g_lpHGE->System_SetState(HGE_HWNDPARENT, hwnd );
		g_lpHGE->System_SetState(HGE_FRAMEFUNC, FrameFunc);
		g_lpHGE->System_SetState(HGE_RENDERFUNC, RenderFunc);
		g_lpHGE->System_SetState(HGE_TITLE, "HGE");
		g_lpHGE->System_SetState(HGE_WINDOWED, true);
		g_lpHGE->System_SetState(HGE_SCREENWIDTH, rct.right-rct.left );
		g_lpHGE->System_SetState(HGE_SCREENHEIGHT, rct.top-rct.bottom );
		g_lpHGE->System_SetState(HGE_SCREENBPP, 32);
		g_lpHGE->System_SetState(HGE_HIDEMOUSE, false);
		g_lpHGE->System_SetState(HGE_FPS, 30);
		g_lpHGE->System_SetState(HGE_TEXTUREFILTER, false );
		g_lpHGE->System_SetState(HGE_DONTSUSPEND, true );
		g_lpHGE->System_SetState(HGE_ZBUFFER, true );


		if(g_lpHGE->System_Initiate())
		{
			hwnd_ = g_lpHGE->System_GetState(HGE_HWND);
			::MoveWindow(parent_hwnd_, 
				rct.left, 
				rct.top,
				rct.right-rct.left, 
				 rct.top-rct.bottom, 
				true);
			::GetClientRect (hwnd_, &rc_client_);

		}

		this->LoadResource();

	}
	catch(...)
	{
		MessageBoxA(NULL,"HGE设置异常" , " 异常 .",MB_OK);
	}
	return true ;
}

//加载资源
bool CHgeView::LoadResource()
{
	g_res = new hgeResourceManager("res.txt");
	sprbg_ = g_res->GetSprite("sprBg");
	if ( sprbg_ == NULL)
	{
		return false ;
	}

	textbg_ = g_res->GetTexture("TexBg");

	sprbg_=new hgeSprite(textbg_,0,0,rc_client_.right-rc_client_.left,rc_client_.top-rc_client_.bottom);
	sprbg_->SetBlendMode(BLEND_COLORADD | BLEND_ALPHABLEND | BLEND_NOZWRITE);
	sprbg_->SetColor(0xFF00FF00,0);
	sprbg_->SetColor(0xFF000000,1);
	sprbg_->SetColor(0xFF0000FF,2);
	sprbg_->SetColor(0xFFFF00FF,3);

	return true ;
}

//更新
void CHgeView::Updata( float dt )
{

}

//绘图
void CHgeView::RenderGameView()
{
	sprbg_->Render(0,0);
}

void CHgeView::RenderGameViewAfterGUI()
{

}

void CHgeView::ShutDown()
{
	g_lpHGE->System_Shutdown();
}


void CHgeView::End()
{
	this->ShutDown();
	g_lpHGE->Release();
}

//帧函数
bool FrameFunc(void)
{
	float fDeltaTime = g_lpHGE->Timer_GetDelta();
	g_lpView->Updata( fDeltaTime );
	return false;
}

//渲染函数
bool RenderFunc(void)
{
	g_lpHGE->Gfx_BeginScene();
	g_lpHGE->Gfx_Clear( 0xFFAAAAAA);	
	g_lpView->RenderGameView();
	g_lpView->RenderGameViewAfterGUI();
	g_lpHGE->Gfx_EndScene();
	return false;
}